<?php
apf_require_class("Bll_BLL");
/**
 * 招聘申请
 * @author dinglin
*/
class Bll_Flow_Recruit extends BLL {
    /**
     * 获取一条招聘申请记录
     * @param int $flow_id
     * @return array
     */
    public function get_recruit_by_id($flow_id){
        $result = array();
        if(!$flow_id || !is_numeric($flow_id)){
            return $result;
        }
        return $this->_get_dao_recruit()->find_by_id($flow_id);
    }
    public function get_recruit_by_ids($flow_ids){
        $result = array();
        if(empty($flow_ids)){
            return $result;
        }
        return $this->_get_dao_recruit()->find_by_ids($flow_ids);
    }
    /**
     * 添加招聘申请记录
     * @param array $data
     * @return int insert_id|0
     */
    public function add_recruit($data){
        if(!$data || !is_array($data)){
            return 0;
        }
        return $this->_get_dao_recruit()->insert($data);
    }
    /**
     * 更新招聘记录
     * @param int $flow_id
     * @param array $data
     * @return int effect rows
     */
    public function update_recruit_by_id($flow_id,$data){
        if(!$data || !is_array($data)){
            return 0;
        }
        if(!$flow_id || !is_numeric($flow_id)){
            return 0;
        }
        return $this->_get_dao_recruit()->update_by_id($flow_id, $data);
    }

    /**
     * 统计汇总的职位信息
     * @param int $jobid
     */
    public function get_recruit_by_jobid($jobid){
        $data = array('pnum'=>0,'fnum'=>0,'anum'=>0);
        if(empty($jobid)){
            return $data;
        }
        $hroa_bll = new Bll_Hr_OutInterface();
        $job_data = $hroa_bll->get_job_dep_info($jobid);
        $data = array(
                'job_info'=>$job_data,
                'pnum'=>intval($job_data['job_plan_num']),
                'fnum'=>0,//intval($job_data['job_fact_num']),
                'anum'=>0//intval($job_data['job_fact_num']),
        );
        //重新计算，职位的在职人数
        $hroa_bll = new Bll_Hr_OutInterface();
        $job_list = $hroa_bll->get_job_user_list(array($jobid));
        if(!empty($job_list)){
            foreach($job_list as $job_id=>$val){
                $data['fnum'] = intval($val['fact_num']);
                $data['anum'] = intval($val['fact_num']);
            }
        }
        if($data['anum']){
            //正在申请离职的人数
            apf_require_class('Bll_Flow_Dimission');
            $dimission_bll = new Bll_Flow_Dimission();
            $num = $dimission_bll->count_dimission_by_job($jobid);
            if($num){
                $data['fnum'] -= $num;
                $data['anum'] -= $num;
            }
        }
        $_where = array(
            "job_id"=>$jobid,
            'apply_status'=>1
        );
        $tmp = $this->_get_dao_recruit()->find_short($_where,'',0,0,array('flow_id','user_type','job_id','job_needs','plan_num','out_plan_num'));
        if(empty($tmp)){
            return $data;
        }
        $recruit_job_data = array();
        foreach($tmp as $val){
            if($val['user_type']==2){//兼职和实习的不算
                $recruit_job_data['plan_num'] += $val['plan_num'];
                $recruit_job_data['plan_num'] += $val['out_plan_num'];
            }
        }
        //实际人数+正在招聘的人数
        $anum = $data['fnum'] + $recruit_job_data['plan_num'];
        $data['anum'] = intval($anum);
        return $data;
    }
    /**
     * 招聘申请记录
     * @return Dao_Flow_Recruit
     */
    private function _get_dao_recruit(){
        return $this->get_dao('Dao_Flow_Recruit');
    }
}